﻿// 功能：弹出式进度条
// 描述：
// 作者：温伟鹏
// 日期：2010/5/15 0:19:26

using System;
using System.Runtime.InteropServices;
using System.Drawing;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 弹出式进度条
    /// </summary>
    public class PopupProgress : Control
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr MzPopupProgressCtor();

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzPopupProgressDispose(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressIsContinue(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetRange(IntPtr pObj, long nMin, long nMax);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetCurrentValue(IntPtr pObj, long nCur);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetProgressType(IntPtr pObj, bool bThin, int nType);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetTitleImage(IntPtr pObj, IntPtr pImage);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr MzPopupProgressGetTitleImage(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetTitleText(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string lpszText);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressGetTitleText(IntPtr pObj, ref IntPtr lpszText);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetNoteImage(IntPtr pObj, IntPtr pImage);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr MzPopupProgressGetNoteImage(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetNoteText(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string lpszText);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressGetNoteText(IntPtr pObj, ref IntPtr strText);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetNoteImage2(IntPtr pObj, IntPtr pImage);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr MzPopupProgressGetNoteImage2(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetNoteText2(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string lpszText);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressGetNoteText2(IntPtr pObj, ref IntPtr strText);

        [DllImport("MZFCWrapper.dll")]
        private static extern uint MzPopupProgressGetSelectButton(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetFirstButtonID(IntPtr pObj, uint nID);

        [DllImport("MZFCWrapper.dll")]
        private static extern uint MzPopupProgressGetFirstButtonID(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressAddBottomButton(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string lpszText, IntPtr pImage, IntPtr pImagePressed);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressSetBGImage(IntPtr pObj, IntPtr hbmpBG);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressStartProgress(IntPtr pObj, IntPtr hWndOwner, bool bUseCancelButton, bool bUseCommandMessage, bool bBGTransparent);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressUpdateProgress(IntPtr pObj, bool bLazy);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzPopupProgressKillProgress(IntPtr pObj);
        #endregion

        #region 私有变量
        private long currentValue;
        private Bitmap backgroundImage;
        private long min;
        private long max;
        private bool bThin;
        private ProgressBarType progressType;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取进度条对话框是否运行
        /// </summary>
        public bool IsContinue
        {
            get
            {
                return MzPopupProgressIsContinue(Handle);
            }
        }
        /// <summary>
        /// 获取或设置当前值
        /// </summary>
        public long Value
        {
            get
            {
                return this.currentValue;
            }
            set
            {
                if (value != this.currentValue)
                {
                    this.currentValue = value;
                    MzPopupProgressSetCurrentValue(Handle, value);
                    Update();
                }
            }
        }
        /// <summary>
        /// 获取或设置进度条上方的标题图片(ImagingHelper类型)
        /// </summary>
        public IntPtr TitleImage
        {
            get
            {
                return MzPopupProgressGetTitleImage(Handle);
            }
            set
            {
                MzPopupProgressSetTitleImage(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置进度条上方的标题文本
        /// </summary>
        public string TitleText
        {
            get
            {
                IntPtr hString = IntPtr.Zero;

                MzPopupProgressGetTitleText(Handle, ref hString);

                return Marshal.PtrToStringUni(hString);
            }
            set
            {
                MzPopupProgressSetTitleText(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置进度条下方的显示图片
        /// </summary>
        public IntPtr NoteImage
        {
            get
            {
                return MzPopupProgressGetNoteImage(Handle);
            }
            set
            {
                MzPopupProgressSetNoteImage(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置进度条下方的显示文本。
        /// 如果NoteText2为空，则NoteText居中显示。
        /// 如果NoteText2不为空，则NoteText与NoteText2在进度条下方左右两侧
        /// </summary>
        public string NoteText
        {
            get
            {
                IntPtr hString = IntPtr.Zero;

                MzPopupProgressGetNoteText(Handle, ref hString);

                return Marshal.PtrToStringUni(hString);
            }
            set
            {
                MzPopupProgressSetNoteText(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置进度条下方的显示图片
        /// </summary>
        public IntPtr NoteImage2
        {
            get
            {
                return MzPopupProgressGetNoteImage2(Handle);
            }
            set
            {
                MzPopupProgressSetNoteImage2(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置进度条下方的显示文本
        /// </summary>
        public string NoteText2
        {
            get
            {
                IntPtr hString = IntPtr.Zero;

                MzPopupProgressGetNoteText2(Handle, ref hString);

                return Marshal.PtrToStringUni(hString);
            }
            set
            {
                MzPopupProgressSetNoteText2(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置第一个按钮的ID
        /// </summary>
        public uint FirstButtonID
        {
            get
            {
                return MzPopupProgressGetFirstButtonID(Handle);
            }
            set
            {
                MzPopupProgressSetFirstButtonID(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置弹出对话框的背景图。
        /// </summary>
        public Bitmap BackgroundImage
        {
            get
            {
                return backgroundImage;
            }
            set
            {
                backgroundImage = value;
                MzPopupProgressSetBGImage(Handle, value.GetHbitmap());
            }
        }
        /// <summary>
        /// 获取或设置进度条最小值
        /// </summary>
        public long Minimum
        {
            get
            {
                return this.min;
            }
            set
            {
                if (value != this.min)
                {
                    if (value > Maximum)
                    {
                        value = Maximum;
                    }
                    min = value;
                    SetRange(value, Maximum);
                }
            }
        }
        /// <summary>
        /// 获取或设置进度条最大值
        /// </summary>
        public long Maximum
        {
            get
            {
                return this.max;
            }
            set
            {
                if (value != this.max)
                {
                    if (value < Minimum)
                    {
                        value = Minimum;
                    }
                    max = value;
                    SetRange(Minimum, value);
                }
            }
        }
        /// <summary>
        /// 获取或设置进度条是否为细样式（当ProgressBarType=Turquoise时，才能设置为细进度条）
        /// </summary>
        public bool ThinBar
        {
            get
            {
                return bThin;
            }
            set
            {
                if (value != bThin)
                {
                    bThin = value;
                    // MZV2_PROGRESSBAR_TYPE_TURQUOISE=0,MZV2_PROGRESSBAR_TYPE_MAX=1
                    MzPopupProgressSetProgressType(Handle, bThin, (int)ProgressBarType);
                }
            }
        }
        /// <summary>
        /// 获取或设置进度条类型
        /// </summary>
        public ProgressBarType ProgressBarType
        {
            get
            {
                return this.progressType;
            }
            set
            {
                if (value != progressType)
                {
                    this.progressType = value;
                    // MZV2_PROGRESSBAR_TYPE_TURQUOISE=0,MZV2_PROGRESSBAR_TYPE_MAX=1
                    MzPopupProgressSetProgressType(Handle, ThinBar, (int)value);
                }
            }
        }
        #endregion

        #region 构造函数

        public PopupProgress()
        {
            Handle = MzPopupProgressCtor();
            SetID();
            min = 0;
            max = 100;
            SetRange(0, 100);
            bThin = false;
            progressType = ProgressBarType.Turquoise;
            SetProgressType(bThin, progressType);
        }
        #endregion

        #region 函数
        /// <summary>
        /// 设置进度条的取值范围
        /// </summary>
        /// <param name="nMin">进度条的最小值</param>
        /// <param name="nMax">进度条的最大值</param>
        /// <returns>设置成功返回true；否则返回false</returns>
        private bool SetRange(long nMin, long nMax)
        {
            return MzPopupProgressSetRange(Handle, nMin, nMax);
        }
        /// <summary>
        /// 设置进度条类型
        /// </summary>
        /// <param name="thinBar">true，表示进度条为细进度条；</param>
        /// <param name="type"></param>
        /// <returns></returns>
        private bool SetProgressType(bool thinBar, ProgressBarType type)
        {
            return MzPopupProgressSetProgressType(Handle, thinBar, (int)type);
        }
        /// <summary>
        /// 在进度条的底部添加一个按钮，每次调用StartProgress之前调用，KillProgress后不再保持已添加的按钮
        /// </summary>
        /// <param name="text"></param>
        public void AddBottomButton(string text)
        {
            AddBottomButton(text, IntPtr.Zero, IntPtr.Zero);
        }
        /// <summary>
        /// 在进度条的底部添加一个按钮，每次调用StartProgress之前调用，KillProgress后不再保持已添加的按钮
        /// </summary>
        /// <param name="text">按钮文本</param>
        /// <param name="pImage">按钮图像</param>
        /// <param name="pImagePressed">按钮按下显示的图片</param>
        public void AddBottomButton(string text, IntPtr pImage, IntPtr pImagePressed)
        {
            bool result = MzPopupProgressAddBottomButton(Handle, text, pImage, pImagePressed);
        }
        /// <summary>
        /// 显示弹出式进度条
        /// </summary>
        /// <param name="hWnd"></param>
        public void Show(IntPtr hWnd)
        {
            Show(hWnd, true, false, false);
        }
        /// <summary>
        /// 显示弹出式进度条
        /// </summary>
        /// <param name="hWnd">弹出窗口的父窗口句柄</param>
        /// <param name="bUseCancelButton">是否使用取消按钮</param>
        /// <param name="bUseCommandMessage">设为TRUE则用户点击按钮时hWndOwner窗口可以接收到MZ_WM_COMMAND消息，否则自动结束窗口</param>
        /// <param name="bBGTransparent">是否使用完全透明的背景</param>
        public void Show(IntPtr hWnd, bool bUseCancelButton, bool bUseCommandMessage, bool bBGTransparent)
        {
            bool result = MzPopupProgressStartProgress(Handle, hWnd, bUseCancelButton, bUseCommandMessage, bBGTransparent);
        }
        /// <summary>
        /// 关闭弹出式进度条
        /// </summary>
        public void Hide()
        {
            bool result = MzPopupProgressKillProgress(Handle);
        }
        /// <summary>
        /// 刷新进度条页面显示，立即刷新
        /// </summary>
        public void Update()
        {
            Update(false);
        }
        /// <summary>
        /// 刷新进度条页面显示
        /// </summary>
        /// <param name="bLazy">设为TRUE则不会立即刷新界面只是设置刷新标志，否则立即刷新。</param>
        public void Update(bool bLazy)
        {
            bool result = MzPopupProgressUpdateProgress(Handle, bLazy);
        }
        #endregion
    }
}
